package com.cn.ccp.action.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.MenuBean;
import com.cn.ccp.bean.RoleBean;
/**
 * 角色action
 * @author renyong
 *
 */
public class RoleAction extends BaseAction{
	private static Logger log = Logger.getLogger(RoleAction.class); // log4j
	private RoleBean roleBean ;
	private List<RoleBean> roleList ;
	
	//角色设置权限
	private SelectItem[] selectMenuItem ;
	private SelectItem[] allMenuItem;

	private String selectRoleMenu ;
	private String rid ;
	private boolean noMenu;
	
	
	private String menuJson ;//tree数据
	
	
	public RoleAction(){
		getpagedetail();
	}
	
	public String edit(){
		boolean flag = getRoleService().roleNameIsAlready(roleBean.getId() ,roleBean.getRolename());
		if(flag){
			showMsg("rolename", "role_page_message_rolename_already");
			return "";
		}
		if(roleBean.getId() == 0)
			return save();
		return update();
	}
	
	public String save(){
		boolean flag = getRoleService().sava(roleBean ,selectRoleMenu);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String update(){
		boolean flag = getRoleService().update(roleBean,selectRoleMenu);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public void delete(){
		FacesContext context = FacesContext.getCurrentInstance();
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// �õ�ҳ��Ĳ���
		getRoleService().delete(Integer.parseInt(id));
		getpagedetail();
	}
	
	public void showBean(){
		FacesContext context = FacesContext.getCurrentInstance();
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		if(id !=null)
			roleBean = (RoleBean) getRoleService().findById(Integer.parseInt(id));
	}
	
	@SuppressWarnings("unchecked")
	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		if(id != null){
			roleBean = (RoleBean) getRoleService().findById(Integer.parseInt(id));
			List<MenuBean> selectMenuList = getMenuService().getSelectRoleMenuList(id);
			selectMenuItem = new SelectItem[selectMenuList.size()];
			int len = selectMenuList.size();
			if(len > 0)
				noMenu = true;
			for (int i = 0; i < len; i++){
				selectMenuItem[i] = new SelectItem(""+selectMenuList.get(i).getId() , selectMenuList.get(i).getMenuName());
			}
			showRoleMenu(id);
		}
		return UPDATE;
	}
	
	public String goback(){
		return MAIN ;
	}
	
	
	public void showRoleMenu(String roleId){
		
		List<Integer> pidList = new ArrayList<Integer>();
		List<MenuBean> selectMenuList = getMenuService().getSelectRoleMenuList(roleId);
		for(MenuBean bean : selectMenuList)
			pidList.add(bean.getParentid());
		List<MenuBean> allMenuList = getMenuService().pageQuery();
		
//		{ id:22, pId:2, name:"随意勾选 2-2", open:true ,checked:true},
//		{ id:221, pId:22, name:"随意勾选 2-2-1", checked:true},
//		{ id:222, pId:22, name:"随意勾选 2-2-2"},
		JSONArray array = new JSONArray();
		for (MenuBean bean : allMenuList) {
			JSONObject json = new JSONObject();
			json.put("id", bean.getId());
			json.put("pId", bean.getParentid());
			json.put("name", bean.getMenuName());
			if(selectMenuList.contains(bean)){
				json.put("checked", "true");
				if(pidList.contains(bean.getParentid()))
					json.put("open", "true");
			}
			array.add(json);
		}
		menuJson = array.toString();
		rid = roleId ;
	}
	
	
	public boolean isNoMenu() {
		return noMenu;
	}

	public void setNoMenu(boolean noMenu) {
		this.noMenu = noMenu;
	}

	@Override
	public void getpagedetail() {
		setItemCount(getRoleService().getCount());
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		roleList = getRoleService().pageQuery(start, pagesize);
	}

	public RoleBean getRoleBean() {
		if(roleBean == null)
			roleBean = new RoleBean();
		return roleBean;
	}

	public void setRoleBean(RoleBean roleBean) {
		this.roleBean = roleBean;
	}

	public List<RoleBean> getRoleList() {
		if(roleList == null)
			roleList = new ArrayList<RoleBean>();
		return roleList;
	}

	public void setRoleList(List<RoleBean> roleList) {
		this.roleList = roleList;
	}

	public SelectItem[] getSelectMenuItem() {
		if(selectMenuItem == null)
			selectMenuItem = new SelectItem[0];
		return selectMenuItem;
	}

	public void setSelectMenuItem(SelectItem[] selectMenuItem) {
		this.selectMenuItem = selectMenuItem;
	}

	public SelectItem[] getAllMenuItem() {
		if(allMenuItem == null)
			allMenuItem = new SelectItem[0];
		return allMenuItem;
	}

	public void setAllMenuItem(SelectItem[] allMenuItem) {
		this.allMenuItem = allMenuItem;
	}
	public String getRid() {
		return rid;
	}

	public void setRid(String rid) {
		this.rid = rid;
	}

	public String getSelectRoleMenu() {
		return selectRoleMenu;
	}

	public void setSelectRoleMenu(String selectRoleMenu) {
		this.selectRoleMenu = selectRoleMenu;
	}

	public String getMenuJson() {
		if(menuJson == null || menuJson.equals("")){
			List<MenuBean> allMenuList = getMenuService().pageQuery();
			JSONArray array = new JSONArray();
			for (MenuBean bean : allMenuList) {
				JSONObject json = new JSONObject();
				json.put("id", bean.getId());
				json.put("pId", bean.getParentid());
				json.put("name", bean.getMenuName());
				array.add(json);
			}
			menuJson = array.toString();
		}
		return menuJson;
	}

	public void setMenuJson(String menuJson) {
		this.menuJson = menuJson;
	}

	
}
